package TCP;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class TcpEchoClient {
    Socket socket=null;
    public TcpEchoClient(String IP,int port) throws IOException {
        socket=new Socket(IP,port);
    }
    //服务器启动
    public void start(){
        System.out.println("客户端启动！");
        Scanner scanner=new Scanner(System.in);
        try (InputStream inputStream=socket.getInputStream();
             OutputStream outputStream=socket.getOutputStream()) {
             PrintWriter writer=new PrintWriter(outputStream);
             Scanner scannernet=new Scanner(inputStream);
            while (true){
                System.out.println("->");
                String request=scanner.next();
                //发送请求
                writer.println(request);
                writer.flush();
                //读取响应
                String response=scannernet.next();
                //打印响应
                System.out.println(response);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) throws IOException {
        TcpEchoClient tcpEchoClient=new TcpEchoClient("127.0.0.1",9090);
        tcpEchoClient.start();
    }

}
